package com.javafactroy.kunlong.test.openfeign;

import cn.hutool.json.JSONUtil;
import com.javafactroy.kunlong.DbAgent;
import com.javafactroy.kunlong.api.dto.DepartmentApiDto;
import com.javafactroy.kunlong.api.factroy.OpenFeignFactroy;

import com.javafactroy.kunlong.api.openfeign.DepartmentOpenApiWebLocal;
import com.javafactroy.kunlong.api.openfeign.DepartmentOpenApiWebRemote;
import com.javafactroy.kunlong.client.feign.dto.ApiResult;
import com.javafactroy.kunlong.client.feign.dto.PageInfoDto;
import com.javafactroy.kunlong.common.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.sql.SQLException;

@SpringBootTest(classes = {DbAgent.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension.class)
@Slf4j
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestOpenFeign {

    @Autowired
    OpenFeignFactroy openFeignFactroy;

    @Autowired
    DepartmentOpenApiWebLocal local;
    @Autowired
    DepartmentOpenApiWebRemote remote;

    @Autowired
    UserConvert userConvert;

    @Test
    public void test000() throws SQLException {
        UseDto useDto = new UseDto();
        useDto.setId("1");
        useDto.setName("leijm");
        useDto.setPageInfoDto(new PageInfoDto());
        useDto.getPageInfoDto().setPageSize(11).setPageNum(2);
        UseEntity entity = userConvert.toOne(useDto);
        log.info("{}", JSONUtil.toJsonPrettyStr( entity));
    }

    @Test
    public void test001_feignLocal9701() throws SQLException {
        ApiResult apiResult = local.query(new DepartmentApiDto());
        log.info("{}", apiResult);
    }

    @Test
    public void test002_feignRemote9701() throws SQLException {
        ApiResult apiResult = remote.query(new DepartmentApiDto());
        log.info("{}", apiResult);
    }

    //@CheckParams
    @Test
    public void test003_feignLocal() throws SQLException {
        ApiResult apiResult = openFeignFactroy.find(DepartmentOpenApiWebLocal.class).query(new DepartmentApiDto());
        log.info("pageinf = {}", CommonUtils.toJSONString(apiResult));
    }

    @Test
    public void test004_feignRemote() throws SQLException {
        ApiResult apiResult = openFeignFactroy.find(DepartmentOpenApiWebRemote.class).query(new DepartmentApiDto());
        log.info("pageinf = {}", CommonUtils.toJSONString(apiResult));
    }

}